<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 会员管理
// +----------------------------------------------------------------------
namespace app\admin\controller\member2;

use app\admin\model\member\Member as MemberModel;
use app\common\controller\Adminbase;
use app\member\service\User;
use think\Db;

class Member extends Adminbase
{
    protected $searchFields = 'id,nickname';
    protected function initialize()
    {
        parent::initialize();
    }
    public function index(){
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $_list=Db::name('member')
                ->where($where)
                ->order('create_time desc')
                ->page($page,$limit)
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['level'] = Db::name('member_level')->find($v['level_id']);
            }
            $total=Db::name('member')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }
    public function edit(){
        $member_id = $this->request->param('id/d', 0);
        $data=Db::name('member')->find($member_id);
        $update=array(
            'id'=>$member_id,
            'status'=>!$data['status'],
        );
        $result=Db::name('member')->update($update);
        if ($result){
            return ['code' => 200, 'data' => '', 'msg' => '切换成功'];
        }else{
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
    public function upedit(){
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            if ($data['invite_id']){
                $up_member=Db::name('member')->find($data['invite_id']);
                if (!$up_member){
                    $this->error('用户不存在');
                }
                if ($up_member['level_id']<2){
                    $this->error('用户等级为游客不能设置');
                }
                Db::name('member')->update([
                    'id'=>$data['id'],
                    'invite_id'=>$data['invite_id'],
                ]);
            }
            $this->success('操作完成');
        }
        $member1=Db::name('member')->find($id);
        $up_member=Db::name('member')->find($member1['invite_id']);
        $this->assign([
            'member_id'=>$id,
            'up_member'=>$up_member
        ]);
        return $this->fetch('');
    }

    public function edit2(){
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data=array(
                'id'=>$data['id'],
                'level_id'=>$data['level_id'],
//                'min_tx'=>$data['min_tx'],
//                'is_clock'=>$data['is_clock'],
//                'is_bargain'=>$data['is_bargain'],
//                'is_pin'=>$data['is_pin'],
            );
            Db::name('member')->update($update_data);
            $this->success('操作成功！');
        }
        $member_level=Db::name('member_level')
            ->where([
                'is_del'=>0
            ])->order('level')
            ->select();
        $data=Db::name('member')->find($id);
        $this->assign([
            'member_level'=>$member_level,
            'data'=>$data
        ]);
        return $this->fetch('');
    }

    //充值1签到 2购物返现 3消费使用 4拼团卷兑换 5购物券兑换 6后台充值
    public function recharge()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $insert_data=array(
                'member_id'=>$data['id'],
                'money'=>$data['money']
            );
            $stmt =addGWCoupons($insert_data['member_id'],$data['money'],'','',6,'后台充值');
            if ($stmt) {
                $this->success('充值完成');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('member')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }
    //1拼团返卷 2拼团使用 3转换购物卷 4后台充值
    public function recharge2()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $insert_data=array(
                'member_id'=>$data['id'],
                'money'=>$data['money']
            );
            $stmt =addPTCoupons($insert_data['member_id'],$data['money'],'',4,'后台充值');
            if ($stmt) {
                $this->success('充值完成');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('member')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }
    //购物卷充值记录
    public function chargelist(){
        $member_id = $this->request->param('member_id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[]=['member_id','=',$member_id];
            $where[]=['type','=',6];
            $_list=Db::name('member_jf')
                ->where($where)
                ->order('create_time desc')
                ->page($page,$limit)
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $total=Db::name('member_jf')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'member_id'=>$member_id
        ]);
        return $this->fetch();
    }
    //拼团卷充值记录
    public function chargelist2(){
        $member_id = $this->request->param('member_id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[]=['member_id','=',$member_id];
            $where[]=['type','=',4];
            $_list=Db::name('member_coupon2')
                ->where($where)
                ->order('create_time desc')
                ->page($page,$limit)
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $total=Db::name('member_coupon2')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'member_id'=>$member_id
        ]);
        return $this->fetch();
    }
    public function manage(){
        $member_id = $this->request->param('id/d', 0);
        $data=Db::name('member')->find($member_id);
        $update=array(
            'id'=>$member_id,
            'is_manage'=>!$data['is_manage'],
        );
        $result=Db::name('member')->update($update);
        if ($result){
            return ['code' => 200, 'data' => '', 'msg' => '设置成功'];
        }else{
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }

    }

}
